<?php

namespace App\Admin\Controllers;

use App\Models\Goods;
use App\Models\OrderGoods;

use App\Models\OrderUser;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Table;


class OrderGoodsController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Request $request)
    {
        $order_id=$request->input('order_id');
        return Admin::content(function (Content $content) use ($order_id){

            $content->header('订单商品列表');
            $content->description('description');

            $content->body($this->grid($order_id));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($order_id)
    {
        Admin::script($this->script());
        return Admin::grid(OrderGoods::class, function (Grid $grid) use ($order_id){
            if($order_id){
                $grid->model()->where('order_id', $order_id);
            }
            $grid->actions(function ($actions){
                $actions->disableEdit();
            });
            $grid->disableCreation();
            $grid->id('ID')->sortable();
            $grid->order_id('关联订单id');
            $grid->user_id('用户id');

            $grid->contact_user_id('匹配用户id')->display(function ($data){
                return $data?:'异常';
            });

            $grid->price('支付单价');
            $grid->contact_user_price('售卖返价');
            $grid->order_user_id('交易状态')->display(function($data){
                $confirm_status=OrderUser::where('id',$data)->value('confirm_status');
                return $confirm_status?'已完成':'未完成';
            });


            $grid->goods('售卖返价')->display(function (){
                $goods=Goods::find($this->goods_id);
                if($goods){
                    $goods_text= "[商品id:$this->goods_id][名称：$goods->title][出版社：$goods->publisher][作者：$goods->author]";
                }else{
                    $goods_text ='数据异常';
                }
                return $goods_text;
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(OrderGoods::class, function (Form $form) {
        });
    }
    protected function script()
    {
        return <<<SCRIPT
$('.grid-expand').on('click', function () {
     if ($(this).data('inserted') == '0') {
        var key = $(this).data('key');
        var row = $(this).closest('tr');
        var html = $('template.grid-expand-'+key).html();

        row.after("<tr><td colspan='"+row.find('td').length+"' style='padding:0 !important; border:0px;'>"+html+"</td></tr>");

        $(this).data('inserted', 1);
    }

    $("i", this).toggleClass("fa-caret-right fa-caret-down");

});

SCRIPT;
    }
}
